package com.approval.approvalapp.center.tz;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.JsdxListBean;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class Jsdx2Activity extends BaseActivity implements LeftAdapter.LeftClick, RightAdapter.ClickListener, Jxdx2SelectAdapter.DelClickListener {
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.lrv)
    RecyclerView lrv;
    @Bind(R.id.rightrv)
    RecyclerView rightrv;
    @Bind(R.id.upv)
    View upv;
    @Bind(R.id.yx)
    TextView yx;
    @Bind(R.id.bm_num)
    TextView bmNum;
    @Bind(R.id.agree_tv)
    LinearLayout agreeTv;
    @Bind(R.id.bottom_rl)
    RelativeLayout bottomRl;
    @Bind(R.id.bm_num1)
    TextView bm_num1;
    @Bind(R.id.blue_down)
    ImageView blue_down;
    @Bind(R.id.rv2)
    RecyclerView rv2;
    @Bind(R.id.root)
    LinearLayout root;
    @Bind(R.id.gray_view)
    View gray_view;
    private ProgressDialog progressDialog;
    private List<JsdxListBean> selectlist;
    private List<JsdxListBean> jsdxListBeans;
    private LeftAdapter leftAdapter;
    private RightAdapter rightAdapter;
    private List<JsdxListBean.ListBean> lists = new ArrayList<>();
    private Jxdx2SelectAdapter jxdx1SelectAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_jsdx2;
    }

    @Override
    public void initView() {
        selectlist = (List<JsdxListBean>) getIntent().getSerializableExtra("selectlist");
        List<String> selenum = new ArrayList<>();
        for (int i = 0; i < selectlist.size(); i++) {
            if (selectlist.get(i).getList() == null) {
                return;
            }
            for (int j = 0; j < selectlist.get(i).getList().size(); j++) {
                if (selectlist.get(i).getList().get(j).isSelect()) {
                    selenum.add("1");
                }
            }
        }
        bmNum.setText(selenum.size() + "人");

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在加载");
        progressDialog.setCancelable(false);
        progressDialog.show();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("areacode", SpUtil.getString(this, SpUtil.REGION_CODE, ""));
        jsonObject.put("is_select", "0");
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/deliver_an_announcement",
                jsonObject.toString(), new NetDownResponse() {

                    @Override
                    public void success(String str) {
                        jsdxListBeans = JSON.parseArray(str, JsdxListBean.class);

                        //二维搞一下
                        for (int i = 0; i < selectlist.size(); i++) {
                            if (selectlist.get(i).getList() == null) {
                                return;
                            }
                            for (int j = 0; j < selectlist.get(i).getList().size(); j++) {
                                if (selectlist.get(i).getList().get(j).isSelect()) {
                                    jsdxListBeans.get(i).getList().get(j).setSelect(true);
                                }
                            }
                        }

                        //给左边第一个true
                        jsdxListBeans.get(0).setSelect(true);

                        lrv.setLayoutManager(new LinearLayoutManager(Jsdx2Activity.this));
                        leftAdapter = new LeftAdapter();
                        lrv.setAdapter(leftAdapter);
                        leftAdapter.setList(jsdxListBeans,Jsdx2Activity.this);

                        rightrv.setLayoutManager(new LinearLayoutManager(Jsdx2Activity.this));
                        rightAdapter = new RightAdapter();
                        rightrv.setAdapter(rightAdapter);
                        rightAdapter.setLists(jsdxListBeans.get(0).getList(),Jsdx2Activity.this);


                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                        }
                    }

                    @Override
                    public void finish() {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                        }
                    }
                });


    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.back_iv, R.id.agree_tv, R.id.bottom_rl, R.id.blue_down})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.agree_tv:
                Intent intent = new Intent();
                //把返回数据存入Intent
                intent.putExtra("selectlist", (Serializable) jsdxListBeans);
                //设置返回数据
                this.setResult(RESULT_OK, intent);
                //关闭Activity
                finish();
                break;
            case R.id.bottom_rl:
                gray_view.setVisibility(View.VISIBLE);
                root.setVisibility(View.VISIBLE);
                jxdx1SelectAdapter = new Jxdx2SelectAdapter();
                rv2.setLayoutManager(new LinearLayoutManager(this));
                rv2.setAdapter(jxdx1SelectAdapter);
                lists = new ArrayList<>();
                for (int i = 0; i < jsdxListBeans.size(); i++) {
                    for (int j = 0; j <jsdxListBeans.get(i).getList().size() ; j++) {
                        if (jsdxListBeans.get(i).getList().get(j).isSelect()) {
                            lists.add(jsdxListBeans.get(i).getList().get(j));
                        }
                    }
                }
                bm_num1.setText(lists.size() + "人");
                jxdx1SelectAdapter.setLists(lists, Jsdx2Activity.this);
                break;
            case R.id.blue_down:
                gray_view.setVisibility(View.GONE);
                root.setVisibility(View.GONE);
                break;
        }
    }

    int leftIndex = 0;

    @Override
    public void leftclick(int position) {
        leftIndex = position;
        for (int i = 0; i <jsdxListBeans.size() ; i++) {
            jsdxListBeans.get(i).setSelect(false);
        }
        jsdxListBeans.get(position).setSelect(true);
        leftAdapter.notifyDataSetChanged();

        rightAdapter.setLists(jsdxListBeans.get(position).getList());

    }

    @Override
    public void rightclick(int position) {
        Log.d("Jsdx2Activity", "position:" + position);
        for (int i = 0; i <jsdxListBeans.get(leftIndex).getList().size() ; i++) {
            if (i == position){
                if (jsdxListBeans.get(leftIndex).getList().get(position).isSelect()) {
                    jsdxListBeans.get(leftIndex).getList().get(position).setSelect(false);
                } else {
                    jsdxListBeans.get(leftIndex).getList().get(position).setSelect(true);
                    Log.d("Jsdx2Activity", "position:" + leftIndex);
                }
            }
        }

            leftAdapter.setList(jsdxListBeans,this);
            rightAdapter.setLists(jsdxListBeans.get(leftIndex).getList());

            List<String> selenum = new ArrayList<>();
            for (int z = 0; z < jsdxListBeans.size(); z++) {
                if (jsdxListBeans.get(z).getList() == null) {
                    return;
                }
                for (int j = 0; j < jsdxListBeans.get(z).getList().size(); j++) {
                    if (jsdxListBeans.get(z).getList().get(j).isSelect()) {
                        selenum.add("1");
                    }
                }
            }
            bmNum.setText(selenum.size() + "人");

    }

    @Override
    public void del(int position) {
        for (int i = 0; i <jsdxListBeans.size() ; i++) {
            for (int j = 0; j < jsdxListBeans.get(i).getList().size(); j++) {
                if (jsdxListBeans.get(i).getList().get(j).getUserid().equals(lists.get(position).getUserid())){
                    jsdxListBeans.get(i).getList().get(j).setSelect(false);
                }
            }
        }




        lists.remove(position);

        leftAdapter.notifyDataSetChanged();
        rightAdapter.notifyDataSetChanged();
        jxdx1SelectAdapter.notifyDataSetChanged();
        bm_num1.setText(lists.size() + "人");
        bmNum.setText(lists.size() + "人");
    }
}
